package com.ruoyi.nongjt.service.impl;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.nongjt.domain.PrinterCertification;
import com.ruoyi.nongjt.mapper.PrinterCertificationMapper;
import com.ruoyi.nongjt.service.PrinterCertificationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 打印机认证情况管理Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-01-01
 */
@Service
public class PrinterCertificationServiceImpl implements PrinterCertificationService 
{
    @Autowired
    private PrinterCertificationMapper printerCertificationMapper;

    /**
     * 查询打印机认证情况管理
     * 
     * @param id 打印机认证情况管理主键
     * @return 打印机认证情况管理
     */
    @Override
    public PrinterCertification selectPrinterCertificationById(Long id)
    {
        return printerCertificationMapper.selectPrinterCertificationById(id);
    }

    /**
     * 查询打印机认证情况管理列表
     * 
     * @param printerCertification 打印机认证情况管理
     * @return 打印机认证情况管理
     */
    @Override
    public List<PrinterCertification> selectPrinterCertificationList(PrinterCertification printerCertification)
    {
        return printerCertificationMapper.selectPrinterCertificationList(printerCertification);
    }

    /**
     * 新增打印机认证情况管理
     * 
     * @param printerCertification 打印机认证情况管理
     * @return 结果
     */
    @Override
    public int insertPrinterCertification(PrinterCertification printerCertification)
    {
        printerCertification.setCreateTime(DateUtils.getNowDate());
        return printerCertificationMapper.insertPrinterCertification(printerCertification);
    }

    /**
     * 修改打印机认证情况管理
     * 
     * @param printerCertification 打印机认证情况管理
     * @return 结果
     */
    @Override
    public int updatePrinterCertification(PrinterCertification printerCertification)
    {
        printerCertification.setUpdateTime(DateUtils.getNowDate());
        return printerCertificationMapper.updatePrinterCertification(printerCertification);
    }

    /**
     * 批量删除打印机认证情况管理
     * 
     * @param ids 需要删除的打印机认证情况管理主键
     * @return 结果
     */
    @Override
    public int deletePrinterCertificationByIds(Long[] ids)
    {
        return printerCertificationMapper.deletePrinterCertificationByIds(ids);
    }

    /**
     * 删除打印机认证情况管理信息
     * 
     * @param id 打印机认证情况管理主键
     * @return 结果
     */
    @Override
    public int deletePrinterCertificationById(Long id)
    {
        return printerCertificationMapper.deletePrinterCertificationById(id);
    }

    /**
     * 校验序列号是否已认证
     * 
     * @param serialNumber 序列号
     * @return 结果
     */
    @Override
    public String checkSerialNumberCertified(String serialNumber)
    {
        int count = printerCertificationMapper.checkSerialNumberCertified(serialNumber);
        return count > 0 ? "1" : "0";
    }

    /**
     * 导入打印机认证情况数据
     * 
     * @param printerCertificationList 打印机认证情况数据列表
     * @param isUpdateSupport 是否更新支持，如果已存在，则进行更新数据
     * @param operName 操作用户
     * @return 结果
     */
    @Override
    public String importPrinterCertification(List<PrinterCertification> printerCertificationList, Boolean isUpdateSupport, String operName)
    {
        if (StringUtils.isNull(printerCertificationList) || printerCertificationList.size() == 0)
        {
            throw new RuntimeException("导入打印机认证情况数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (PrinterCertification printerCertification : printerCertificationList)
        {
            try
            {
                // 验证是否存在这个序列号的认证记录
                PrinterCertification p = printerCertificationMapper.selectPrinterCertificationBySerialNumber(printerCertification.getSerialNumber());
                if (StringUtils.isNull(p))
                {
                    printerCertification.setCreateBy(operName);
                    this.insertPrinterCertification(printerCertification);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、序列号 " + printerCertification.getSerialNumber() + " 导入成功");
                }
                else if (isUpdateSupport)
                {
                    printerCertification.setId(p.getId());
                    printerCertification.setUpdateBy(operName);
                    this.updatePrinterCertification(printerCertification);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、序列号 " + printerCertification.getSerialNumber() + " 更新成功");
                }
                else
                {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、序列号 " + printerCertification.getSerialNumber() + " 已存在");
                }
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、序列号 " + printerCertification.getSerialNumber() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new RuntimeException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
}
